
//////////////////////////////////////////////////////////////////////
#ifndef _SCENEMANAGER_H_
#define _SCENEMANAGER_H_


#include "config.h"
#include "d3dheaders.h"
#include "video/RaySceneQuery.h"
#include "defines.h"
#include "video/Light.h"

namespace KEngine
{
	class Device;
	class Camera;
	class SceneNode;
	class RenderQueue;
	class Skybox;
	class Terrain;
	class Water;
	class Application;
	class TextureManager;
	class Entity;

	class _KExport SceneManager
	{
	public:
		SceneManager(Application* app);
		virtual ~SceneManager();

		void setCamera(Camera* camera);
		Device* getDevice() const
		{
			return _device;
		}

		void intersects(RaySceneQuery& raySceneQuery);
		void update(unsigned long elapsedTime);
		void render(TimeValue t);

		SceneNode* getRootSceneNode();

		virtual RenderQueue* getRenderQueue();

		void usetLights();
		bool useAmbientLight();

		void useFog(bool use = true);
		void setFog(unsigned long mode,ColorValue color,float linearStart = 150,float linearEnd = 2000,float expDensity = 0.1);
	
		void setSkybox(Skybox* skyBox);
		Skybox* getSkybox()const {return _skybox;}
		void setTerrain(Terrain* terrain);
		Terrain* getTerrain()const {return _terrain;}
		void setWater(Water* water);
		Water* getWater() const {return _water;}

		void setAmbientLightColor(const ColorValue& color){_ambientLight = color;}
		void addLight(const D3DLight& lt);

		void destory();

		TextureManager* getTextureManager();

		Entity* createEntity(const char* name);
	protected:
		virtual void	_prepareRenderQueue();
		virtual void	_findVisibleObjects(Camera * cam);
		virtual void	_renderVisibleObjects(TimeValue t);

	protected:
		//render device!
		Device* _device;

		//scene system
		Camera* _camera;
		SceneNode* _sceneRoot;
		
		//queue for rendering
		RenderQueue* _renderQueue; 

		//light
		ColorValue _ambientLight;
		LightList _lightList;
		unsigned short	_currentLights;

		void _setD3D9Light(unsigned long index, const D3DLight* lt);

		//fog
		// Fog
		unsigned long	mFogMode;
		ColorValue		mFogColour;
		float			mFogStart;
		float			mFogEnd;
		float			mFogDensity;

		//skybox
		Skybox*			_skybox;
		Terrain*		_terrain;
		Water*			_water;

		bool			_reflectMovables;

		//app
		Application*	_app;
	};
	
};

#endif

